<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>components</title>
  <script src="/js/vue.min.js"></script>
</head>

<body>
  <div id="components">
    <div id="button-counter">
      <button-counter></button-counter>
      <button-counter></button-counter>
      <button-counter></button-counter>
    </div>

    <div id="blog-post" :style="{ fontSize: postFontSize + 'em' }">
      <blog-post v-for="(post, index) in posts" :key="index" :post="post" :title="post.title"
        @enlarge-text="computeFontSize" @narrow-text="computeFontSize"></blog-post>
    </div>
  </div>
</body>

<script>
  // 定义一个名为 button-counter 的新组件
  Vue.component('button-counter', {
    data() {
      return {
        cnt: 0
      }
    },
    template: `\
    <button v-on:click="cnt++">\
      You clicked me {{ cnt }} times.
    </button>`,
  })

  Vue.component('blog-post', {
    props: ['post'],
    template: `<div>
    <h3>{{ post.title }}</h3>
    <p>{{ post.content }}</p>
    <button @click="$emit('enlarge-text', 0.2)">Enlarge text</button>
    <button @click="$emit('narrow-text', -0.1)">narrow text</button>
    <div>`
  })

  let app = new Vue({
    el: '#components',
    data: {
      posts: [
        {
          title: 'My journey ..',
          content: 'My journey with Vue',
        },
        {
          title: 'Why Vue ..',
          content: 'Why Vue is so fun',
        },
        {
          title: 'Blogging ..',
          content: 'Blogging with Vue',
        }
      ],
      postFontSize: 1
    },
    methods: {
      computeFontSize(size) {
        this.postFontSize += size
      }
    },
  })
</script>

</html>
